﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Diagnostics;

namespace CoayBookDownloader
{
    class ParseWeb
    {
        private const String CONST_UA="Mozilla/5.0 (Windows NT 6.1; wow64) Applewebkit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36";
        private String m_Url;
        private CookieContainer m_CookieContainer;
        public ParseWeb(String Url,ref CookieContainer cookieContainer)
        {
            m_Url = Url;
            m_CookieContainer = cookieContainer;
        }

        public List<ParseWebResult> StartParse()
        {
            List<ParseWebResult> retlist=new List<ParseWebResult>();

            HttpWebResponse result = null;

            try
            {
                Uri ourUri = new Uri(m_Url);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(ourUri);
                req.Headers.Add("DNT","1");
                req.UserAgent=CONST_UA;
                req.CookieContainer=m_CookieContainer;
                result = (HttpWebResponse)req.GetResponse();
                //check header
                //result.GetResponseHeader();
                //result.get

                Stream ReceiveStream = result.GetResponseStream();

                //read the stream into a string
                StreamReader sr = new StreamReader(ReceiveStream);
                string resultstring = sr.ReadToEnd();

                Util.WriteToFile(resultstring, "test.html");

            }
            catch (WebException e)
            {
                Trace.WriteLine(e.Message);
                HttpWebResponse response = (HttpWebResponse)e.Response;
                if (response!=null)
                {
                    switch (response.StatusCode)
                    {
                        case HttpStatusCode.NotFound:
                            break;
                    }
                }
            }
            catch (Exception e)
            {
                Trace.WriteLine(e.Message);
            }
            finally
            {
                if (result != null)
                {
                    result.Close();
                }
            }

            try
            {
                // Create a New WebClient object.
                WebClient client = new WebClient();
                client.Headers.Add("user-agent", );
                client.Headers.Add("DNT", "1");
                Stream data = client.OpenRead(new Uri(m_Url));
                StreamReader reader = new StreamReader(data);
                string s = reader.ReadToEnd();

                Util.WriteToFile(s,"test.html");

                data.Close();
                reader.Close();
            }
            catch (System.Exception ex)
            {
                Trace.WriteLine(ex.ToString());

                return retlist;
            }

            return retlist;
        }
    }
}
